import Vue from 'vue'
import VueRouter from 'vue-router'
// 模块之间需要引入才能使用，引入仓库
import store from '@/store'
// 引入组件
import Layout from '@/views/layout/IndexVue'
import Login from '@/views/login/LoginVue'
import Dashboard from '@/views/dashboard/IndexVue'

import OperationOrderlist from '@/views/operationorderlist/index.vue'
import MaintenanceOrder from '@/views/maintenanceorder/index.vue'

import AreaManagement from '@/views/areamanagement/index.vue'
import PointManagement from '@/views/pointmanagement/index.vue'
import PartnerManage from '@/views/partnermanage/index.vue'

import DeviceManagelist from '@/views/devicemanagelist/index.vue'
import DeviceStatus from '@/views/devicestatus/index.vue'
import DeviceTypemanage from '@/views/devicetypemange/index.vue'

import Personlistlist from '@/views/personlistlist/index.vue'
import PersonTotal from '@/views/persontotal/index.vue'
import WorkList from '@/views/worklist/index.vue'

import GoodsType from '@/views/goodstype/index.vue'
import GoodsManage from '@/views/goodsmanage/index.vue'
import PolicyManage from '@/views/policymanage/index.vue'
import OrderManage from '@/views/ordermanage/index.vue'
import MoneyManage from '@/views/moneymanage/index.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [{
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: '/dashboard',
      component: Dashboard
    },
    {
      path: '/operationorderlist',
      component: OperationOrderlist
    },
    {
      path: '/maintenanceorder',
      component: MaintenanceOrder
    },
    {
      path: '/areamanagement',
      component: AreaManagement
    },
    {
      path: '/pointmanagement',
      component: PointManagement
    },
    {
      path: '/partnermanage',
      component: PartnerManage
    },
    {
      path: '/devicemanagelist',
      component: DeviceManagelist
    },
    {
      path: '/devicestatus',
      component: DeviceStatus
    },
    {
      path: '/devicetypemanage',
      component: DeviceTypemanage
    },
    {
      path: '/personlistlist',
      component: Personlistlist
    },
    {
      path: '/persontotal',
      component: PersonTotal
    },
    {
      path: '/worklist',
      component: WorkList
    },
    {
      path: '/goodstype',
      component: GoodsType
    },
    {
      path: '/goodsmanage',
      component: GoodsManage
    },
    {
      path: '/policymanage',
      component: PolicyManage
    },
    {
      path: '/ordermanage',
      component: OrderManage
    },
    {
      path: '/moneymanage',
      component: MoneyManage
    }
    ]
  }
  ]
})
const whiteList = ['/login']
// 路由前置守卫，每次进入一个路由地址，都会触发这个函数
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
